// Caesar Cipher example
// This shows a way to encrypt simple strings.
// The secret key that you choose needs to be in the range 1 to 26
// By Ben 22:01 20/10/2018
#include <iostream>
using namespace std;

void Caesar(string &src, int shift, bool decrypt = false){
	int y = 0;
	for (int i = 0; i < src.length(); i++){
		//Check that we have letters only
		if (isalpha(src[i])){
			//Test for lowercase
			if (islower(src[i])){
				if (!decrypt){
					//Encrypt
					y = src[i] + shift;
					//Check if more than z
					if (y > 'z'){
						y = y - 'z' + 'a' - 1;
					}
					src[i] = (char)y;
				}
				else{
					//Decrypt
					y = src[i] - shift;
					//Check if y is less than a
					if (y < 'a'){
						y = y + 'z' - 'a' + 1;
					}
					src[i] = (char)y;
				}
			}
			else{
				//Check for uppercase letters
				if (!decrypt){
					//Encrypt plus shift value to src i
					y = src[i] + shift;
					//Check if more than z
					if (y > 'Z'){
						y = y - 'Z' + 'A' - 1;
					}
					src[i] = (char)y;
				}
				else{
					//Decrypt
					y = src[i] - shift;
					if (y < 'A'){
						y = y + 'Z' - 'A' + 1;
					}
					src[i] = (char)y;
				}
			}
		}
	}
}


int main(int argc, char **argv) {
	string s0 = "I like learning to code on SoloLearn.";
	int secret = 18;
	//Encrypt
	Caesar(s0, secret);
	std::cout << "Encrypted  : " << s0.c_str() << endl;
	//Decrypt
	Caesar(s0, secret, true);
	std::cout << "Decrypted  : " << s0.c_str() << endl;

	system("pause");
	return 0;
}